<template>
  <div class="app_container">
    <el-card class="box-card" v-if="!detailVisible && !configVisible">
      <div slot="header" class="header clearfix">
        <el-button type="primary" @click="handleAdd1"
          >&emsp;新增&emsp;
        </el-button>
      </div>
      <div class="item" v-loading="listLoading">
        <el-table
          ref="dataList"
          :data="menuList"
          style="width:100%;margin-bottom: 20px;"
          row-key="id"
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column prop="id" label="序号"></el-table-column>
          <el-table-column prop="name" label="赛季名称"></el-table-column>
          <el-table-column prop="time_from" label="开始时间"></el-table-column>
          <el-table-column prop="time_to" label="结束时间"></el-table-column>
          <el-table-column
            prop="advance_price"
            label="进阶版价格"
          ></el-table-column>

          <el-table-column label="操作" width="270px">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="mini"
                @click="handleEdit(scope.row)"
                plain
                >编辑
              </el-button>
              <el-button
                type="text"
                size="mini"
                @click="handleDetail(scope.row)"
                plain
                >查看详情
              </el-button>
              <el-button
                type="text"
                size="mini"
                @click="handleConfig(scope.row.id)"
                plain
                >宣传礼物配置
              </el-button>
              <el-button
                type="text"
                size="mini"
                @click="handleDelete(scope.row)"
                plain
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.perPage"
          @pagination="getList"
        />
      </div>
    </el-card>

    <el-card class="box-card" v-if="detailVisible">
      <div slot="header" class="header clearfix">
        <span>所属赛季：{{ sessionName }}</span>
        <el-button
          style="float:right"
          type="danger"
          @click="detailVisible = false"
          >&emsp;返回&emsp;
        </el-button>
        <el-button type="primary" @click="handleAdd2"
          >&emsp;新增&emsp;
        </el-button>
        <el-button type="primary" @click="handleExport">
          一键导入上期
        </el-button>
      </div>
      <div class="item" v-loading="listLoading">
        <el-table
          ref="dataList"
          :data="menuList2"
          style="width:100%;margin-bottom: 20px;"
          row-key="id"
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <!-- <el-table-column prop="id" label="序号"> </el-table-column> -->
          <el-table-column prop="level" label="战令等级"></el-table-column>
          <el-table-column prop="experience" label="所需经验"></el-table-column>
          <el-table-column prop="price" label="所需金币"></el-table-column>
          <el-table-column label="普通奖励">
            <template slot-scope="scoped">
              <div
                v-if="scoped.row.normal.length > 0"
                style="display:flex;flex-direction:column;justify-content:center;align-items:center"
              >
                <el-popover placement="right" max-width="420" trigger="hover">
                  <img
                    :src="scoped.row.normal[0].picture"
                    alt=""
                    style="max-width: 200px;max-height: 200px"
                  />
                  <img
                    slot="reference"
                    :src="scoped.row.normal[0].picture"
                    alt=""
                    style="width: 50px;"
                  />
                </el-popover>
                <p>{{ scoped.row.normal[0].name }}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="进阶奖励一">
            <template slot-scope="scoped">
              <div
                v-if="scoped.row.special.length > 0"
                style="display:flex;flex-direction:column;justify-content:center;align-items:center"
              >
                <el-popover placement="right" max-width="420" trigger="hover">
                  <img
                    :src="scoped.row.special[0].picture"
                    alt=""
                    style="max-width: 200px;max-height: 200px"
                  />
                  <img
                    slot="reference"
                    :src="scoped.row.special[0].picture"
                    alt=""
                    style="width: 50px;"
                  />
                </el-popover>
                <p>{{ scoped.row.special[0].name }}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="进阶奖励二">
            <template slot-scope="scoped">
              <div
                v-if="scoped.row.special.length > 1"
                style="display:flex;flex-direction:column;justify-content:center;align-items:center"
              >
                <el-popover placement="right" max-width="420" trigger="hover">
                  <img
                    :src="scoped.row.special[1].picture"
                    alt=""
                    style="max-width: 200px;max-height: 200px"
                  />
                  <img
                    slot="reference"
                    :src="scoped.row.special[1].picture"
                    alt=""
                    style="width: 50px;"
                  />
                </el-popover>
                <p>{{ scoped.row.special[1].name }}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180px">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="handleEdit2(scope.row)"
                plain
                >编辑
              </el-button>
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="handleDelete2(scope.row)"
                plain
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <el-card class="box-card" v-if="configVisible">
      <div slot="header" class="header clearfix">
        <span>所属赛季：{{ sessionName }}</span>
        <el-button
          style="float:right"
          type="danger"
          @click="configVisible = false"
          >&emsp;返回&emsp;
        </el-button>
        <el-button type="primary" @click="handleAdd3"
          >&emsp;新增&emsp;
        </el-button>
      </div>
      <div class="item" v-loading="listLoading">
        <el-table
          ref="dataList"
          :data="menuList3"
          style="width:100%;margin-bottom: 20px;"
          row-key="id"
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column prop="id" label="序号"></el-table-column>
          <el-table-column prop="name" label="礼物名称"></el-table-column>
          <el-table-column prop="picture" label="礼物图片">
            <template slot-scope="scoped">
              <el-popover placement="right" max-width="420" trigger="hover">
                <img
                  :src="scoped.row.picture"
                  alt=""
                  style="max-width: 200px;max-height: 200px"
                />
                <img
                  slot="reference"
                  :src="scoped.row.picture"
                  alt=""
                  style="width: 50px;"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100px">
            <template slot-scope="scope">
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="handleDelete3(scope.row)"
                plain
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <!-- <cus-pagination
            :total="total3"
            :page.sync="listQuery3.page"
            :limit.sync="listQuery3.perPage"
            @pagination="getList"
        /> -->
      </div>
    </el-card>

    <!-- 赛季管理新增 -->
    <el-dialog
      :visible.sync="dialogFormVisible1"
      :title="titleMap[dialogStatus]"
      @close="handleDialogClose('editRulesForm1')"
    >
      <el-form
        :model="editRulesForm1"
        :rules="editRulesFormRules1"
        ref="editRulesForm1"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="赛季名称" prop="name">
          <el-input
            v-model="editRulesForm1.name"
            placeholder="请输入赛季名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="赛季开始时间" prop="time_from">
          <el-date-picker
            v-model="editRulesForm1.time_from"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="赛季结束时间" prop="time_to">
          <el-date-picker
            v-model="editRulesForm1.time_to"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="进阶版价格" prop="advance_price">
          <el-input
            v-model="editRulesForm1.advance_price"
            placeholder="请输入进阶版价格"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible1 = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm1')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 战令管理新增 -->
    <el-dialog
      :visible.sync="dialogFormVisible2"
      :title="titleMap[dialogStatus]"
      @close="handleDialogClose('editRulesForm2')"
    >
      <el-form
        :model="editRulesForm2"
        :rules="editRulesFormRules2"
        ref="editRulesForm2"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="战令等级" prop="level">
          <el-input
            v-model="editRulesForm2.level"
            placeholder="请输入战令等级"
          ></el-input>
        </el-form-item>
        <el-form-item label="所需经验" prop="experience">
          <el-input
            v-model="editRulesForm2.experience"
            placeholder="请输入所需经验"
          ></el-input>
        </el-form-item>
        <el-form-item label="所需金币" prop="price">
          <el-input
            v-model="editRulesForm2.price"
            placeholder="请输入所需金币"
          ></el-input>
        </el-form-item>
        <el-form-item label="普通版奖励">
          <el-select
            filterable
            v-model="editRulesForm2.reward_id1"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in giftType"
              :key="'普通' + item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量">
          <el-input
            v-model="editRulesForm2.number1"
            placeholder="请输入奖励数量"
          ></el-input>
        </el-form-item>
        <el-form-item label="进阶版奖励一">
          <el-select
            filterable
            v-model="editRulesForm2.reward_id2"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in giftType"
              :key="'特殊一' + item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量">
          <el-input
            v-model="editRulesForm2.number2"
            placeholder="请输入奖励数量"
          ></el-input>
        </el-form-item>
        <el-form-item label="进阶版奖励二">
          <el-select
            filterable
            v-model="editRulesForm2.reward_id3"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in giftType"
              :key="'特殊二' + item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量">
          <el-input
            v-model="editRulesForm2.number3"
            placeholder="请输入奖励数量"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible2 = false">返回</el-button>
          <el-button type="primary" @click="submitForm2('editRulesForm2')"
            >提交
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 进阶礼物宣传新增 -->
    <el-dialog
      :visible.sync="dialogFormVisible3"
      :title="titleMap[dialogStatus]"
      @close="handleDialogClose('editRulesForm3')"
    >
      <el-form
        :model="editRulesForm3"
        :rules="editRulesFormRules3"
        ref="editRulesForm3"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="奖励" prop="id">
          <el-select
            filterable
            v-model="editRulesForm3.id"
            placeholder="请选择"
            @change="handleChange"
            clearable
          >
            <el-option
              v-for="item in rewardType"
              :key="'宣传' + item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible3 = false">返回</el-button>
          <el-button type="primary" @click="submitForm3('editRulesForm3')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 删除弹框 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
    >
      <span style=""
        ><em style="font-style:normal;color:red">删除后不可恢复</em
        >，你还要继续吗？</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listData,
  giftListData,
  listDetailData,
  postData,
  postOrderData,
  listRewardData,
  postRewardData,
  deleteData,
  deleteOrderData,
  postImport
} from "@/api/warOrder/warOrderList";

export default {
  data() {
    return {
      confirmBtn: false,
      delete_type: undefined,
      deleteId: undefined,
      deleteDialogVisible: false,
      rewardType: [],
      sessionId: undefined,
      sessionName: undefined,
      editRulesFormRules3: {
        id: [{ required: true, message: "请选择宣传礼物", trigger: "change" }]
      },
      editRulesForm3: {
        id: undefined,
        detail_id: undefined
      },
      dialogFormVisible3: false,
      editRulesFormRules2: {
        level: [{ required: true, message: "请输入战令等级", trigger: "blur" }],
        experience: [
          { required: true, message: "请输入升级战令所需经验", trigger: "blur" }
        ],
        price: [
          { required: true, message: "请输入升级战令所需金币", trigger: "blur" }
        ]
      },
      editRulesForm2: {
        id: undefined,
        level: undefined,
        experience: undefined,
        price: undefined,
        reward_id1: undefined,
        reward_id2: undefined,
        reward_id3: undefined,
        number1: undefined,
        number2: undefined,
        number3: undefined
      },
      dialogFormVisible2: false,
      editRulesFormRules1: {
        name: [{ required: true, message: "请输入赛季名称", trigger: "blur" }],
        advance_price: [
          { required: true, message: "请输入进阶版价格", trigger: "blur" }
        ],
        time_from: [
          { required: true, message: "请选择日期", trigger: "change" }
        ],
        time_to: [{ required: true, message: "请选择日期", trigger: "change" }]
      },
      editRulesForm1: {
        id: undefined,
        name: undefined,
        advance_price: undefined,
        time_from: undefined,
        time_to: undefined
      },
      dialogStatus: "",
      // dialog的头部信息
      titleMap: {
        update: "编辑",
        create: "添加"
      },
      dialogFormVisible1: false,

      giftType: [],

      detailVisible: false,
      configVisible: false,

      listLoading: false,
      menuList: [],
      listQuery: {
        page: 1,
        perPage: 10
      },
      total: 0,
      menuList2: [],
      listQuery2: {
        page: 1,
        perPage: 10
      },
      total2: 0,
      menuList3: [],
      listQuery3: {
        page: 1,
        perPage: 10
      },
      total3: 0,
      currentRowData: {}, // 当前被编辑行数据

      sessionInfo: {}
    };
  },
  created() {
    this.getList();
    this.getGiftList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      let urlData = "?";
      if (this.listQuery.page) {
        urlData = urlData + `page=${this.listQuery.page}&`;
      }
      if (this.listQuery.perPage) {
        urlData = urlData + `perPage=${this.listQuery.perPage}`;
      }
      if (this.listQuery.name) {
        urlData = urlData + `&name=${this.listQuery.name}`;
      }
      listData(urlData).then(response => {
        this.menuList = response.data.list;
        this.total = response.data.meta.total;
        this.listLoading = false;
        this.confirmBtn = false;
      });
    },
    getGiftList() {
      giftListData().then(response => {
        response.data.data.forEach((item, index) => {
          this.giftType.push({ value: item.id, label: item.name });
        });
      });
    },
    handleAdd1() {
      this.dialogStatus = "create";
      this.resetForm1("editRulesForm1");
      this.dialogFormVisible1 = true;
    },
    handleEdit(row) {
      this.editRulesForm1.id = row.id;
      this.editRulesForm1.name = row.name;
      this.editRulesForm1.advance_price = row.advance_price;
      this.editRulesForm1.time_from = new Date(row.time_from);
      this.editRulesForm1.time_to = new Date(row.time_to);

      this.dialogStatus = "update";
      this.dialogFormVisible1 = true;
    },
    handleDetail(row) {
      this.sessionInfo = row;
      this.detailVisible = true;
      this.sessionName = row.name;
      this.sessionId = row.id;
      console.log(this.sessionId, "ididi");
      listDetailData(this.sessionId).then(response => {
        this.menuList2 = response.data;
        console.log(response.data, "data");
      });
    },
    //详情页 一键导入
    handleExport() {
      postImport(this.sessionInfo.id).then(res => {
        if (res.code === 200) {
          this.$message.success("操作成功");
          this.handleDetail(this.sessionInfo);
        }
      });
    },
    handleConfig(id) {
      this.configVisible = true;
      this.sessionId = id;
      let query = `?id=${this.sessionId}&recommend=1`;
      listRewardData(query).then(response => {
        this.menuList3 = response.data;
      });
    },
    handleDelete(row) {
      this.delete_type = 1;
      this.deleteId = row.id;
      this.deleteDialogVisible = true;
    },
    handleDelete2(row) {
      this.delete_type = 2;
      this.deleteId = row.id;
      this.deleteDialogVisible = true;
    },
    handleDelete3(row) {
      this.delete_type = 3;

      this.editRulesForm3.id = row.id;
      this.editRulesForm3.detail_id = row.detail_id;

      this.deleteDialogVisible = true;
    },
    // 确定删除
    confirmDelete() {
      if (this.confirmBtn) return;
      this.confirmBtn = true;
      if (this.delete_type === 1) {
        /* let formData = new FormData()
        formData.append("id",this.deleteId) */
        let obj = { id: this.deleteId };
        deleteData(obj).then(response => {
          if (response.code === 200) {
            this.getList();
            this.confirmBtn = false;
            this.deleteDialogVisible = false;
            this.$notify({
              type: "success",
              message: "操作成功",
              position: "bottom-right",
              showClose: false,
              duration: 3000
            });
          } else {
            this.confirmBtn = false;
          }
        });
      } else if (this.delete_type === 2) {
        /* let formData = new FormData()
        formData.append("id",this.deleteId) */
        let obj = { id: this.deleteId };
        deleteOrderData(obj).then(response => {
          if (response.code === 200) {
            this.handleDetail(this.sessionInfo);
            this.deleteDialogVisible = false;
            this.confirmBtn = false;
            this.$notify({
              type: "success",
              message: "操作成功",
              position: "bottom-right",
              showClose: false,
              duration: 3000
            });
          } else {
            this.confirmBtn = false;
          }
        });
      } else if (this.delete_type === 3) {
        let formData = new FormData();
        formData.append("id", this.editRulesForm3.id);

        formData.append("detail_id", this.editRulesForm3.detail_id);
        formData.append("recommend", 0);
        postRewardData(formData).then(response => {
          if (response.code === 200) {
            this.handleConfig(this.sessionInfo);
            this.deleteDialogVisible = false;
            this.confirmBtn = false;
            this.$notify({
              type: "success",
              message: "操作成功",
              position: "bottom-right",
              showClose: false,
              duration: 3000
            });
          } else {
            this.confirmBtn = false;
          }
        });
      }
    },
    resetForm1(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
      });
      this.editRulesForm1 = {
        id: undefined,
        name: undefined,
        advance_price: undefined,
        time_from: undefined,
        time_to: undefined
      };
    },
    submitForm(formName) {
      if (this.confirmBtn) return;
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.confirmBtn = true;
          if (this.dialogStatus === "create") {
            let formData = new FormData();
            for (let item in this.editRulesForm1) {
              this.editRulesForm1[item] === undefined
                ? ""
                : formData.append(item, this.editRulesForm1[item]);
            }
            postData(formData).then(response => {
              if (response.code === 200) {
                this.getList();
                this.confirmBtn = false;
                this.dialogFormVisible1 = false;
                this.$notify({
                  type: "success",
                  message: "操作成功",
                  position: "bottom-right",
                  showClose: false,
                  duration: 3000
                });
              } else {
                this.confirmBtn = false;
              }
            });
          } else {
            let formData = new FormData();
            for (let item in this.editRulesForm1) {
              // 增加判断条件 判断当前是 时间 并且不是字符串 进行更改为字符串
              if (
                item === "time_from" ||
                (item === "time_to" &&
                  typeof this.editRulesForm1[item] !== "string")
              ) {
                let timer =
                  this.editRulesForm1[item].getFullYear() +
                  "-" +
                  (this.editRulesForm1[item].getMonth() + 1 + "").padStart(
                    2,
                    0
                  ) +
                  "-" +
                  String(this.editRulesForm1[item].getDate()).padStart(2, 0);
                formData.append(item, timer);
              } else
                this.editRulesForm1[item] === undefined
                  ? ""
                  : formData.append(item, this.editRulesForm1[item]);
            }
            postData(formData).then(response => {
              if (response.code === 200) {
                this.getList();
                this.confirmBtn = false;
                this.dialogFormVisible1 = false;
                this.$notify({
                  type: "success",
                  message: "操作成功",
                  position: "bottom-right",
                  showClose: false,
                  duration: 3000
                });
              } else {
                this.confirmBtn = false;
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    handleAdd2() {
      this.dialogStatus = "create";
      this.resetForm2("editRulesForm2");
      this.dialogFormVisible2 = true;
    },
    handleEdit2(row) {
      this.editRulesForm2.id = row.id;
      this.editRulesForm2.level = row.level;
      this.editRulesForm2.experience = row.experience;
      this.editRulesForm2.price = row.price;
      if (row.normal.length > 0) {
        this.editRulesForm2.reward_id1 = row.normal[0].item_id;
        this.editRulesForm2.number1 = row.normal[0].number;
      }
      if (row.special.length > 0) {
        this.editRulesForm2.reward_id2 = row.special[0].item_id;
        this.editRulesForm2.number2 = row.special[0].number;
      }
      if (row.special.length > 1) {
        this.editRulesForm2.reward_id3 = row.special[1].item_id;
        this.editRulesForm2.number3 = row.special[1].number;
      }
      this.currentRowData = row;
      this.dialogStatus = "update";
      this.dialogFormVisible2 = true;
    },
    resetForm2(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
      });
      this.editRulesForm2 = {
        id: undefined,
        level: undefined,
        experience: undefined,
        price: undefined,
        reward_id1: undefined,
        reward_id2: undefined,
        reward_id3: undefined,
        number1: undefined,
        number2: undefined,
        number3: undefined
      };
    },
    submitForm2(formName) {
      // if(this.confirmBtn) return
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.confirmBtn = true;
          if (this.dialogStatus === "create") {
            let formData = new FormData();
            formData.append("war_order_id", this.sessionId);
            formData.append("level", this.editRulesForm2.level);
            formData.append("experience", this.editRulesForm2.experience);
            formData.append("price", this.editRulesForm2.price);
            let arr = [];
            if (this.editRulesForm2.reward_id1 && this.editRulesForm2.number1) {
              arr.push({
                type: 1,
                reward_id: this.editRulesForm2.reward_id1,
                number: this.editRulesForm2.number1
              });
            }
            if (this.editRulesForm2.reward_id2 && this.editRulesForm2.number2) {
              arr.push({
                type: 2,
                reward_id: this.editRulesForm2.reward_id2,
                number: this.editRulesForm2.number2
              });
            }
            if (this.editRulesForm2.reward_id3 && this.editRulesForm2.number3) {
              arr.push({
                type: 3,
                reward_id: this.editRulesForm2.reward_id3,
                number: this.editRulesForm2.number3
              });
            }
            if (arr.length > 0) {
              formData.append("rewards", JSON.stringify(arr));
            }

            postOrderData(formData).then(response => {
              if (response.code === 200) {
                this.confirmBtn = false;
                console.log(this.sessionId);
                this.handleDetail(this.sessionInfo);
                this.dialogFormVisible2 = false;
                this.$notify({
                  type: "success",
                  message: "操作成功",
                  position: "bottom-right",
                  showClose: false,
                  duration: 3000
                });
              } else {
                this.confirmBtn = false;
              }
            });
          } else {
            let formData = new FormData();
            formData.append("id", this.editRulesForm2.id);
            formData.append("war_order_id", this.sessionId);
            formData.append("level", this.editRulesForm2.level);
            formData.append("experience", this.editRulesForm2.experience);
            formData.append("price", this.editRulesForm2.price);
            let arr = [];
            if (this.editRulesForm2.reward_id1 && this.editRulesForm2.number1) {
              arr.push({
                id:
                  this.currentRowData["normal"][0] &&
                  this.currentRowData["normal"][0]["id"],
                type: 1,
                reward_id: this.editRulesForm2.reward_id1,
                number: this.editRulesForm2.number1
              });
            }
            if (this.editRulesForm2.reward_id2 && this.editRulesForm2.number2) {
              arr.push({
                id:
                  this.currentRowData["special"][0] &&
                  this.currentRowData["special"][0]["id"],
                type: 2,
                reward_id: this.editRulesForm2.reward_id2,
                number: this.editRulesForm2.number2
              });
            }
            if (this.editRulesForm2.reward_id3 && this.editRulesForm2.number3) {
              arr.push({
                id:
                  this.currentRowData["special"][1] &&
                  this.currentRowData["special"][0] &&
                  this.currentRowData["special"][1]["id"],
                type: 3,
                reward_id: this.editRulesForm2.reward_id3,
                number: this.editRulesForm2.number3
              });
            }
            if (arr.length > 0) {
              formData.append("rewards", JSON.stringify(arr));
            }

            postOrderData(formData).then(response => {
              if (response.code === 200) {
                this.confirmBtn = false;
                this.handleDetail(this.sessionInfo);
                this.dialogFormVisible2 = false;
                this.$notify({
                  type: "success",
                  message: "操作成功",
                  position: "bottom-right",
                  showClose: false,
                  duration: 3000
                });
              } else {
                this.confirmBtn = false;
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    handleAdd3() {
      this.rewardType = [];
      let str = `?id=${this.sessionId}`;
      listRewardData(str).then(response => {
        response.data.forEach((item, index) => {
          this.rewardType.push({
            value: item.id,
            label: item.name,
            detail_id: item.detail_id
          });
        });
        this.dialogStatus = "create";
        this.resetForm3("editRulesForm3");
        this.dialogFormVisible3 = true;
      });
    },
    getRewardList() {
      let str = `?id=${this.sessionId}`;
      listRewardData(str).then(response => {
        response.data.forEach((item, index) => {
          this.rewardType.push({
            value: item.item_id,
            label: item.name,
            detail_id: item.id
          });
        });
      });
    },
    resetForm3(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
      });
      this.editRulesForm3 = {
        id: undefined,
        detail_id: undefined
      };
    },
    handleChange() {
      this.rewardType.forEach(item => {
        if (item.value === this.editRulesForm3.id) {
          this.editRulesForm3.detail_id = item.detail_id;
        }
      });
    },
    submitForm3(formName) {
      if (this.confirmBtn) return;

      this.$refs[formName].validate(valid => {
        if (valid) {
          this.confirmBtn = true;
          // alert('submit!');
          if (this.dialogStatus === "create") {
            let formData = new FormData();
            formData.append("id", this.editRulesForm3.id);

            formData.append("detail_id", this.editRulesForm3.detail_id);
            formData.append("recommend", 1);

            postRewardData(formData).then(response => {
              if (response.code === 200) {
                this.confirmBtn = false;
                this.handleConfig(this.sessionId);
                this.dialogFormVisible3 = false;
                this.$notify({
                  type: "success",
                  message: "操作成功",
                  position: "bottom-right",
                  showClose: false,
                  duration: 3000
                });
              } else {
                this.confirmBtn = false;
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      this.dialogStatus = "create";
      this.editRulesForm1 = {
        id: undefined,
        name: undefined,
        advance_price: undefined,
        time_from: undefined,
        time_to: undefined
      };

      this.editRulesForm2 = {
        id: undefined,
        level: undefined,
        experience: undefined,
        price: undefined,
        reward_id1: undefined,
        reward_id2: undefined,
        reward_id3: undefined,
        number1: undefined,
        number2: undefined,
        number3: undefined
      };

      this.editRulesForm3 = {
        id: undefined,
        detail_id: undefined
      };

      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate(); // 清除整个表单的校验
        this.$refs[formName].resetFields();
      }
    }
  }
};
</script>

<style scoped lang="scss">
[v-cloak] {
  display: none;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/ .el-card__header {
  padding-bottom: 0;
}

.header {
  margin-bottom: 20px;
}
</style>
